//
//  FriendsCell.m
//  FreeTime
//
//  Created by  on 16/8/30.
//  Copyright © 2016年 SRDZ.com. All rights reserved.
//

#import "FriendsCell.h"
#import "Header.h"

@interface FriendsCell (){
    UIButton * iconBtn,*commentBtn,*likeBtn;
    UILabel * nameLbl,*timeLbl,*contentLbl;
    NSIndexPath * indexPathCell;
    NSMutableArray * imgBtnArr;
}

@end

@implementation FriendsCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        iconBtn = [[UIButton alloc] init];
        iconBtn.backgroundColor = DEBUG_CLOLR;
        iconBtn.tag = 202;
        [iconBtn addTarget:self action:@selector(commentAndLikeBtn:) forControlEvents:UIControlEventTouchUpInside];

        [self addSubview:iconBtn];
        
        self.imgView = [[UIView alloc] init];
//        _imgView.backgroundColor = [UIColor redColor];
        [self addSubview:_imgView];
        
        self.commentView = [[UIView alloc] init];
//        _commentView.backgroundColor = [UIColor redColor];
        [self addSubview:_commentView];
        
        commentBtn = [[UIButton alloc] init];
        commentBtn.layer.borderColor = [ImageUtil colorWithRgb:0xebebeb].CGColor;
        commentBtn.layer.borderWidth = 1;
        [commentBtn setTitleColor:[ImageUtil colorWithRgb:0x999999] forState:UIControlStateNormal];
        commentBtn.tag = 200;
        [commentBtn addTarget:self action:@selector(commentAndLikeBtn:) forControlEvents:UIControlEventTouchUpInside];
        commentBtn.layer.cornerRadius = 5;
        commentBtn.titleLabel.font = FONT(13);
        [self addSubview:commentBtn];
        
        likeBtn = [[UIButton alloc] init];
        likeBtn.layer.borderColor = [ImageUtil colorWithRgb:0xebebeb].CGColor;
        likeBtn.layer.borderWidth = 1;
        [likeBtn addTarget:self action:@selector(commentAndLikeBtn:) forControlEvents:UIControlEventTouchUpInside];
        likeBtn.tag = 201;
        likeBtn.layer.cornerRadius = 5;
        likeBtn.titleLabel.font = FONT(13);
        [likeBtn setTitleColor:[ImageUtil colorWithRgb:0x999999] forState:UIControlStateNormal];
        [self addSubview:likeBtn];
        
        nameLbl = [[UILabel alloc] init];
        nameLbl.font = [UIFont boldSystemFontOfSize:14];
        nameLbl.textColor = [UIColor blackColor];
        [self addSubview:nameLbl];
        
        timeLbl = [UILabel new];
        timeLbl.font = FONT(14);
        timeLbl.textAlignment = NSTextAlignmentRight;
        timeLbl.textColor = [ImageUtil colorWithRgb:0x999999];
        [self addSubview:timeLbl];
        
        contentLbl = [UILabel new];
        contentLbl.font = FONT(14);
        contentLbl.numberOfLines = 0;
        contentLbl.textColor = [ImageUtil colorWithRgb:0x666666];
        [self addSubview:contentLbl];
    }
    return self;
}
- (void)bindData:(NSDictionary *)dic indexPath:(NSIndexPath *)indexPath{
    indexPathCell = indexPath;
    iconBtn.frame = [dic[@"iconFrame"] CGRectValue];
    nameLbl.frame = [dic[@"nameFrame"] CGRectValue];
    timeLbl.frame = [dic[@"timeFrame"] CGRectValue];
    contentLbl.frame = [dic[@"contentFrame"] CGRectValue];
    commentBtn.frame = [dic[@"commentFrame"] CGRectValue];
    likeBtn.frame = [dic[@"likeFrame"] CGRectValue];
    [ToolUtil createLine:[dic[@"lineFrame"] CGRectValue].origin.y superView:self.contentView lift:0 width:[dic[@"lineFrame"] CGRectValue].size.width hight:[dic[@"lineFrame"] CGRectValue].size.height color:[ImageUtil colorWithRgb:0xe6e6e6]];
    [iconBtn setBackgroundImage:[UIImage imageNamed:dic[@"iconImg"]] forState:UIControlStateNormal];
    iconBtn.layer.cornerRadius = 25;
    iconBtn.layer.masksToBounds = YES;
    nameLbl.text = dic[@"name"];
    timeLbl.text = dic[@"time"];
    contentLbl.text = dic[@"content"];
    [commentBtn setTitle:@"私密评论" forState:UIControlStateNormal];
    [likeBtn setTitle:@"喜欢" forState:UIControlStateNormal];
    self.imgView.frame = [dic[@"imgViewFrame"] CGRectValue];
    
    for (UIView * view  in dic[@"commentArr"]) {
        [self.commentView addSubview:view];
    }
    for (UIButton * btn in dic[@"imgArr"]) {
        [btn addTarget:self action:@selector(imgBtn:) forControlEvents:UIControlEventTouchUpInside];

        [self.imgView addSubview:btn];
    }
}
+ (NSMutableDictionary *)cellFrame:(NSMutableDictionary *)dic indexPath:(NSIndexPath *)indexPath
{
    
    dic = [FriendsCell turn:dic indexPath:indexPath];

    CGRect iconFrame = CGRectMake(10, 20, 50, 50);
    CGFloat w = [ToolUtil autoWidth:dic[@"name"] width:0 height:15 num:14];
    CGRect nameFrame = CGRectMake(CGRectGetMaxX(iconFrame)+10, iconFrame.origin.y+5, w+10, 15);
    CGFloat w1 = [ToolUtil autoWidth:dic[@"time"] width:0 height:10 num:14];
    CGRect timeFrame = CGRectMake(WindowWidth-w1-10, nameFrame.origin.y, w1, 10);
    CGFloat y = [ToolUtil customAutoHeigh:dic[@"content"] width:WindowWidth-nameFrame.origin.x-10 num:14];
    CGRect contentFrame = CGRectMake(nameFrame.origin.x, CGRectGetMaxY(iconFrame)-13, WindowWidth-nameFrame.origin.x-10, y);

    
    NSDictionary * dict = [FriendsCell loadView:dic withFrame:contentFrame];
    CGRect  imgViewFrame = CGRectMake(nameFrame.origin.x, CGRectGetMaxY(contentFrame)+10, WindowWidth-nameFrame.origin.x-10, [dict[@"viewHeight"] floatValue]);
    CGFloat y2 = 0;
    if (imgViewFrame.size.height == 0) {
        y2=CGRectGetMaxY(contentFrame);
        
    }else{
        y2=CGRectGetMaxY(imgViewFrame);
    }
    
    CGRect commentFrame = CGRectMake(nameFrame.origin.x, y2+10, 80, 30);
    CGRect likeFrame = CGRectMake(CGRectGetMaxX(commentFrame)+10, y2+10, 60, 30);
 
    NSDictionary * dicti =  [FriendsCell loadCommentView:dic withFrame:likeFrame];
    CGFloat y1 = CGRectGetMaxY(likeFrame)+10+[dicti[@"viewHeight"] intValue];
//    if ([dicti[@"viewHeight"] intValue] == 0) {
//        y1=CGRectGetMaxY(likeFrame)+10;
//    }else{
//        y1=cg
//    }
    CGRect lineFrame = CGRectMake(0, y1, WindowWidth, 10);
    CGFloat cellHeight = CGRectGetMaxY(lineFrame);
    [dic setObject:dicti[@"commentArr"] forKey:@"commentArr"];
    [dic setObject:dict[@"imgArr"] forKey:@"imgArr"];
    [dic setObject:[NSValue valueWithCGRect:imgViewFrame] forKey:@"imgViewFrame"];
    [dic setObject:[NSValue valueWithCGRect:iconFrame] forKey:@"iconFrame"];
    [dic setObject:[NSValue valueWithCGRect:nameFrame] forKey:@"nameFrame"];
    [dic setObject:[NSValue valueWithCGRect:timeFrame] forKey:@"timeFrame"];
    [dic setObject:[NSValue valueWithCGRect:contentFrame] forKey:@"contentFrame"];
    [dic setObject:[NSValue valueWithCGRect:commentFrame] forKey:@"commentFrame"];
    [dic setObject:[NSValue valueWithCGRect:likeFrame] forKey:@"likeFrame"];
    [dic setObject:[NSValue valueWithCGRect:lineFrame] forKey:@"lineFrame"];
    [dic setObject:[NSNumber numberWithFloat:cellHeight] forKey:@"cellHeight"];

    return dic;
}

+ (NSMutableDictionary*)turn:(NSMutableDictionary*)dict indexPath:(NSIndexPath *)indexPath{
    
    return [NSMutableDictionary dictionaryWithDictionary:dict];
}

+ (NSDictionary * )loadView:(NSDictionary *)dict withFrame:(CGRect)frame{
    NSMutableArray * imgArr = [NSMutableArray array];
    CGFloat w2 = (WindowWidth - frame.origin.x-30)/3;
    int count = 0;
    int num = 0;
    CGFloat y2 = 0;
    CGFloat y3 = 0;
    if ([dict[@"image"] intValue]<=0) {
        y3=0;
    }else if ([dict[@"image"] intValue]>0 &&[dict[@"image"] intValue]<=2) {
        y3=75;
    }else if ([dict[@"image"] intValue]>2 && [dict[@"image"] intValue]<=5){
        y3=75*2+10;
    }else {
        y3=75*3+20;
        
    }
    for (int i = 0; i<[dict[@"image"] intValue]; i++) {
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeSystem];
        btn.frame = CGRectMake(count*10+count*w2, num*85, w2, 75);
        btn.tag = 100+i;
        UIImageView * img = [ReduceCode creatImageVeiwWithFrame:CGRectMake(0, 0, w2, 75) imageName:@"123" isCorner:NO];
        [btn addSubview:img];
        [imgArr addObject:btn];
        count++;
        
        if (i== 2) {
            num++;
            count =0;
        }else if (i == 5){
            num++;
            count =0;
        }else if (i == 8){
            num++;
            count =0;
        }
        y2=CGRectGetMaxY(btn.frame);
        
    }

    return @{@"height":[NSNumber numberWithFloat:y2],@"imgArr":imgArr,@"viewHeight":[NSNumber numberWithFloat:y3]};
}
+ (NSDictionary * )loadCommentView:(NSDictionary *)dict withFrame:(CGRect)frame{
    CGFloat y1 = 0;
    y1 = [dict[@"commentNum"] intValue] *50;
    NSMutableArray * commentViewArr = [NSMutableArray array];
    for (int i = 0; i< [dict[@"commentNum"] intValue]; i++) {
        UIView * bgView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(frame)+10+i*50, WindowWidth, 50)];
        bgView.backgroundColor = DEBUG_CLOLR;
        [ToolUtil createLine:49.5 superView:bgView lift:0 width:WindowWidth hight:0.5 color:[ImageUtil colorWithRgb:0xe6e6e6]];
        [commentViewArr addObject:bgView];
    }
    return @{@"commentArr":commentViewArr,@"viewHeight":[NSNumber numberWithFloat:y1]};
}
#pragma mark -- 响应事件 --
- (void)commentAndLikeBtn:(UIButton *)btn{
    [self.delegate didSelectLikeAndCommentBtn:btn cell:self indexPath:indexPathCell];
}

- (void)imgBtn:(UIButton *)btn{
    
    [self.delegate didSelectBtn:btn cell:self imageArr:nil];
}
@end
